package com.bc004346.travelus.DataAccess.AsyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.DataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Model.Domain.FlightSearchParams;
import com.bc004346.travelus.Model.Interface.ISearchFlightsCallback;
import com.bc004346.travelus.Utility.Utility;

import java.util.ArrayList;

import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 3/12/14.
 * Search for flights
 */
public class SearchFlightsTaskFragment extends Fragment {

    //<editor-fold desc="Members">
    private static final String PARAMS = "params";
    private static final String IS_RETURN_SEARCH = "isReturnSearch";
    private ISearchFlightsCallback mCallbacks;
    private TravelUsScreenScraper mTravelUS;
    private FlightSearchParams mParams;
    private Boolean mIsReturnSearch;
    //</editor-fold>

    public static SearchFlightsTaskFragment newInstance(FlightSearchParams params, Boolean isReturnSearch) {
        SearchFlightsTaskFragment f = new SearchFlightsTaskFragment();
        Bundle args = new Bundle();
        args.putSerializable(PARAMS, params);
        args.putBoolean(IS_RETURN_SEARCH, isReturnSearch);
        f.setArguments(args);
        return f;
    }

    //<editor-fold desc="Overrides">
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (ISearchFlightsCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUS = Utility.getInstance().getTravelUS();
        mParams = getArguments() == null
                ? null
                : (FlightSearchParams) getArguments().getSerializable(PARAMS);
        mIsReturnSearch = getArguments() == null || getArguments().getBoolean(IS_RETURN_SEARCH);
        new SearchTask().execute();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }
    //</editor-fold>

    private class SearchTask extends AsyncTask<Void, Void, ArrayList<Flight>> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(ArrayList<Flight> flights) {
            if (mCallbacks != null) {
                mCallbacks.onSearchPostExecute(flights, ex);
            }
        }

        @Override
        protected ArrayList<Flight> doInBackground(Void... params) {
            ArrayList<Flight> flights = null;
            try {
                if (mIsReturnSearch)
                    flights = mTravelUS.searchReturnFlights(mParams);
                else
                    flights = mTravelUS.searchFlights(mParams);
            } catch (Exception e) {
                log(String.format("Exception: %s", e.toString()));
                ex = e;
            }
            return flights;
        }
    }
}
